package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.query.QueryObjext;
import cn.wolfcode.p2p.bussness.domain.Bid;
import cn.wolfcode.p2p.bussness.mapper.BidMapper;
import cn.wolfcode.p2p.bussness.service.IBidService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service@Transactional
public class BidServiceImpl implements IBidService {
    @Autowired
    private BidMapper bidMapper;

    @Override
    public int save(Bid record) {
        return bidMapper.insert(record);
    }

    @Override
    public Bid get(Long id) {
        return bidMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Bid record) {
        int count = bidMapper.updateByPrimaryKey(record);
        if (count == 0) {
            throw new DisplayableExcetion("乐观锁异常,recordId="+record.getId());
        }
        return count;
    }

    @Override
    public PageInfo queryPage(QueryObjext qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List list = bidMapper.selectAll(qo);
        return new PageInfo(list);
    }

    @Override
    public void updateState(Long bidRequestId, int bidRequestState) {
        bidMapper.updateState(bidRequestId,bidRequestState);
    }
}
